// 今天
var date = new Date();
var hour = date.getHours();
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDate();
var _month = (month + 1) < 10 ? "0" + (month + 1) : month + 1;
var _day = day < 10 ? "0" + day : day;
var today = year + "-" + _month + "-" + _day;
var showToday = _month + "月" + _day + "日";

// 明天
date = new Date(year, month, day + 1);
year = date.getFullYear();
month = date.getMonth();
day = date.getDate();
_month = (month + 1) < 10 ? "0" + (month + 1) : month + 1;
_day = day < 10 ? "0" + day : day;
var tomorrow = year + "-" + _month + "-" + _day;
var showTomorrow = _month + "月" + _day + "日";

var todayFlag = false;
var tomorrowFlag = false;
var timeAllFlag = false;
var timeAmFlag = false;
var timePmFlag = false;
var timeNightFlag = false;
var editionAllFlag = false;
var edition1Flag = false;
var edition2Flag = false;
var edition3Flag = false;

var args = {};
var param = {reqData : args, reqUrl : "queryPlay"};

$(function()
{
	var html = "<a onclick=\"setCss(this,'today','" + today + "')\">今天(" + showToday + ")</a>&nbsp;<a onclick=\"setCss(this,'tomorrow','" + tomorrow + "')\">明天(" + showTomorrow + ")</a>";
	$("#spDate").html(html);
	
	args.movieName = $("#hdMovieName").val();
	
	queryPlay();
});

setCss = function(obj, type, val)
{
	/******************日期*******************/
	if(type == "today")
	{
		todayFlag = !todayFlag;
		
		if(todayFlag)
		{
			setSelectedCss(obj);
			args.today = val;
		}
		else
		{
			setNotSelectedCss(obj);
			delete args.today;
		}
	}

	if(type == "tomorrow")
	{
		tomorrowFlag = !tomorrowFlag;
		
		if(tomorrowFlag)
		{
			setSelectedCss(obj);
			args.tomorrow = val;
		}
		else
		{
			setNotSelectedCss(obj);
			delete args.tomorrow;
		}
	}
	
	/******************时间*******************/
	if(type == "timeAll")
	{
		timeAllFlag = !timeAllFlag;
		
		if(timeAllFlag)
		{
			setSelectedCss(obj);
			args.timeAll = val;
			
			timeAmFlag = false;
			timePmFlag = false;
			timeNightFlag = false;
			
			setNotSelectedCss($("#div_time a:eq(1)"));
			setNotSelectedCss($("#div_time a:eq(2)"));
			setNotSelectedCss($("#div_time a:eq(3)"));

			delete args.timeAm;
			delete args.timePm;
			delete args.timeNight;
		}
		else
		{
			setNotSelectedCss(obj);
			delete args.timeAll;
		}
	}
	
	if(type == "timeAm")
	{
		timeAmFlag = !timeAmFlag;
		
		if(timeAmFlag)
		{
			setSelectedCss(obj);
			args.timeAm = val;
			
			timeAllFlag = false;
			setNotSelectedCss($("#div_time a:eq(0)"));
			delete args.timeAll;
		}
		else
		{
			setNotSelectedCss(obj);
			delete args.timeAm;
		}
	}
	
	if(type == "timePm")
	{
		timePmFlag = !timePmFlag;
		
		if(timePmFlag)
		{
			setSelectedCss(obj);
			args.timePm = val;
			
			timeAllFlag = false;
			setNotSelectedCss($("#div_time a:eq(0)"));
			delete args.timeAll;
		}
		else
		{
			setNotSelectedCss(obj);
			delete args.timePm;
		}
	}
	
	if(type == "timeNight")
	{
		timeNightFlag = !timeNightFlag;
		
		if(timeNightFlag)
		{
			setSelectedCss(obj);
			args.timeNight = val;
			
			timeAllFlag = false;
			setNotSelectedCss($("#div_time a:eq(0)"));
			delete args.timeAll;
		}
		else
		{
			setNotSelectedCss(obj);
			delete args.timeNight;
		}
	}
	
	/******************版本*******************/
	if(type == "editionAll")
	{
		editionAllFlag = !editionAllFlag;
		
		if(editionAllFlag)
		{
			setSelectedCss(obj);
			args.editionAll = val;
			
			edition1Flag = false;
			edition2Flag = false;
			edition3Flag = false;
			
			setNotSelectedCss($("#div_edition a:eq(1)"));
			setNotSelectedCss($("#div_edition a:eq(2)"));
			setNotSelectedCss($("#div_edition a:eq(3)"));

			delete args.edition1;
			delete args.edition2;
			delete args.edition3;
		}
		else
		{
			setNotSelectedCss(obj);
			delete args.editionAll;
		}
	}
	
	if(type == "edition1")
	{
		edition1Flag = !edition1Flag;
		
		if(edition1Flag)
		{
			setSelectedCss(obj);
			args.edition1 = val;
			
			editionAllFlag = false;
			setNotSelectedCss($("#div_edition a:eq(0)"));
			delete args.editionAll;
		}
		else
		{
			setNotSelectedCss(obj);
			delete args.edition1;
		}
	}
	
	if(type == "edition2")
	{
		edition2Flag = !edition2Flag;
		
		if(edition2Flag)
		{
			setSelectedCss(obj);
			args.edition2 = val;
			
			editionAllFlag = false;
			setNotSelectedCss($("#div_edition a:eq(0)"));
			delete args.editionAll;
		}
		else
		{
			setNotSelectedCss(obj);
			delete args.edition2;
		}
	}
	
	if(type == "edition3")
	{
		edition3Flag = !edition3Flag;
		
		if(edition3Flag)
		{
			setSelectedCss(obj);
			args.edition3 = val;
			
			editionAllFlag = false;
			setNotSelectedCss($("#div_edition a:eq(0)"));
			delete args.editionAll;
		}
		else
		{
			setNotSelectedCss(obj);
			delete args.edition3;
		}
	}
	
	queryPlay();
}

setSelectedCss = function(obj)
{
	$(obj).css("backgroundColor", "#3399FF");
	$(obj).css("color", "white");
}

setNotSelectedCss = function(obj)
{
	$(obj).css("backgroundColor", "");
	$(obj).css("color", "#3399FF");
}

queryPlay = function()
{
	if(args.movieName)
	{
		if(!todayFlag && !tomorrowFlag)
		{
			args.today = today;
			args.tomorrow = tomorrow;
		}
		else if(!todayFlag)
		{
			delete args.today;
		}
		else if(!tomorrowFlag)
		{
			delete args.tomorrow;
		}
		
		ajaxRequest(param, function(returnData)
		{
			if(returnData)
			{
				$("table tr:gt(0)").empty();
				
				var trs = [];
				
				$(returnData).each(function()
				{
					var newtime = parseInt(this.playTime2);
					var newhour = parseInt(hour);

					if(newhour<newtime){
						var playTime2 = this.playTime2;
						var ticket = this.ticket;
						var languageName = this.languageName;
						var editionName = this.editionName;
						var playPrice = this.playPrice;
						var playId = this.playId;
						
						var tds = new Array();
						tds.push("<td>" + playTime2 + "</td>");
						tds.push("<td>" + ticket + "</td>");
						tds.push("<td>" + languageName + "</td>");
						tds.push("<td>" + editionName + "</td>");
						tds.push("<td>" + playPrice + "</td>");
						tds.push("<td><a onclick=\"buyTicket(" + playId + ")\">我要购票</a></td>");
						
						var tr = "<tr>" + tds.join("") + "</tr>";
						trs.push(tr);
					}
				});
				
				$("table").append(trs.join(""));
				
				$("table").show();
				$("#div_play_msg").hide();
			}
			else
			{
				$("#div_play_msg").show();
				$("table").hide();
			}
		}, null);
	}	
}

buyTicket = function(playId)
{
	var memberId = $("#hdMemberId").val();

	var json = {memberId : memberId, playId : playId};
	
	/*
	 * var json = "{memberId:\"" + memberId + "\",playId:\"" + playId + "\"}";
	 * window.location = "login.jsp?json=" + json;
	 * json字符串转换成json对象,需在json字符外包裹一对小括号:eval('(' + json + ')')
	 */

	if(!memberId)
	{
		window.location = "login.jsp?json=" + $.toJSON(json);
		return;
	}
	
	window.location = "seat.jsp?json=" + $.toJSON(json);
}